package cn.ava.org.service.impl;


import cn.ava.basic.constant.BaseConstants;
import cn.ava.basic.exception.BusinessException;
import cn.ava.basic.service.impl.BaseServiceImpl;
import cn.ava.basic.utils.BaiDuAiCensorUtil;
import cn.ava.basic.utils.SendmailUtil;
import cn.ava.basic.vo.ShopEchartsVo;
import cn.ava.org.domain.Employee;
import cn.ava.org.domain.Shop;
import cn.ava.org.domain.ShopAuditLog;
import cn.ava.org.mapper.ShopAuditLogMapper;
import cn.ava.org.mapper.ShopMapper;
import cn.ava.org.service.IShopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @ClassName: ShopServiceImpl
 * @Description: 店铺服务层实现类
 * @Author: Avavaava
 * @Date: 2022/7/1 9:25
 * @version: 23.53
 *
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired  // 店铺map层
    private ShopMapper shopMapper;

    @Autowired // 员工map层
    private EmployeeServiceImpl employeeService;

    @Autowired // 人工审核日志管理map层
    private ShopAuditLogMapper shopAuditLogMapper;

    /**
     * @Title: settlement
     * @Description: 店铺入驻业务逻辑流程
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/12 11:17
     * @param:
     * @return:
     */
    @Override
    @Transactional
    public void settlement(Shop shop) {
        // 首先判断参数是否为空
        if (StringUtils.isNotEmpty(shop.getName())
                || StringUtils.isNotEmpty(shop.getAddress())
                || StringUtils.isNotEmpty(shop.getTel()) )
        {
            throw new BusinessException("输入的参数不能为空,请重新输入");
        }
        // 获取管理员参数
        Employee admin = shop.getAdmin();
        // 判断两次输入密码是否一致
        if (!admin.getPassword().equals(admin.getComfirmPassword())) {
            throw new BusinessException("输入的密码不一致,请重新输入");
        }
        // 调用百度AI审核判断参数是否合法
        if (!BaiDuAiCensorUtil.TextCensor(shop.getName().trim())
                || !BaiDuAiCensorUtil.TextCensor(admin.getUsername().trim()))
        {
            throw new BusinessException("输入的参数不合法,请重新输入");
        }
        // 正则表达式校验手机号和邮箱
        if (!Pattern.matches(BaseConstants.VerifyShopSettlement.REGEX_MOBILE,admin.getPhone()))
        {
            throw new BusinessException("请输入正确的手机号,请重新输入");
        }else if(!Pattern.matches(BaseConstants.VerifyShopSettlement.REGEX_EMAIL,admin.getEmail()))
        {
            throw new BusinessException("请输入正确的邮箱,请重新输入");
        }
        // 调用百度AI审核判断图片是否合法
        String imgPath = BaseConstants.VerifyShopSettlement.FASTDFS_IMAGE + shop.getLogo(); // 拼接Fastdfs地址
        if (!BaiDuAiCensorUtil.ImgCensor(imgPath)) {
            throw new BusinessException("传入的图片不合法,请重新上传!");
        }
        // 先查询出店铺是否存在
        Shop byShop = shopMapper.getByName(shop.getName());
        // 判断店铺是否已经注册
        if (byShop != null) {
            throw new BusinessException("店铺已经存在,请重新更换!");
        }
        // 查询管理员是否存在,如果存在是否还绑定了店铺
        Employee empTmp = employeeService.getByUsername(admin.getUsername());
        
        if (empTmp != null && empTmp.getShopId() != null) {
            throw new BusinessException("该账号已绑定其他店铺,请重新更换!");
        }
        // 判断如果管理员没有注册,则保存
        if (empTmp == null) {
            employeeService.add(admin);
            empTmp = admin;
        } // 如果存在则修改
        // 设置店铺管理员id
        shop.setAdminId(empTmp.getId());
        shopMapper.add(shop);
        // 设置管理员的管理店铺id
        empTmp.setShopId(shop.getId());
        employeeService.update(empTmp);
    }

    /**
     * @Title: pass
     * @Description: 店铺审核通过邮件
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/12 11:17
     * @param:
     * @return:
     */
    @Override
    @Transactional
    public void pass(ShopAuditLog shopAuditLog) {
        // 查询出对应店铺数据
        Shop shop = shopMapper.getById(Long.valueOf(shopAuditLog.getShopId()));
        // 设置店铺状态为未审核
        shop.setState(0);
        // 设置审核人员id
        shopAuditLog.setAuditId(0);
        // 调用mapper查询出对应的管理员
        Employee admin = employeeService.getById(shop.getAdminId());
        // 设置店铺通过状态状态
        shop.setState(1);
        SendmailUtil.sendMail
                ("2353421050@qq.com"
                        ,admin.getEmail() // 这里是查询出来管理员的邮箱
                        ,"店铺审核结果通知!"
                        ,"<h1>你好,你的店铺申请已通过!</h1>"+
                                "点击下方链接激活店铺<br>" +
                                "<a href='http://localhost:8081/#/ActivateShop?shopId="+shopAuditLog.getShopId() +" '>激活链接</a>"
                        ,true);
        // 把对应审核的信息保存进对应的状态
        shopAuditLogMapper.add(shopAuditLog);
        shopMapper.update(shop);
    }

    /**
     * @Title: reject
     * @Description: 店铺审核驳回邮件
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/12 11:17
     * @param:
     * @return:
     */
    @Override
    @Transactional
    public void reject(ShopAuditLog shopAuditLog) {
        // 查询出对应店铺数据
        Shop shop = shopMapper.getById(Long.valueOf(shopAuditLog.getShopId()));
        // 设置店铺状态为未审核
        shop.setState(0);
        // 设置审核人员id
        shopAuditLog.setAuditId(1); // 登录未实现先把操作人id写死
        // 调用mapper查询出对应的管理员
        Employee admin = employeeService.getById(shop.getAdminId());
        // 设置店铺驳回状态
        shop.setState(-1);
        SendmailUtil.sendMail
                ("2353421050@qq.com"
                        ,admin.getEmail()
                        ,"店铺审核结果通知!"
                        ,"<h1>你好,你的店铺申请已被驳回!</h1>"  +
                                "驳回原因:"+shopAuditLog.getNote()+"<br>" +
                                "点击下方链接重新修改店铺信息<br>" +
                                "<a href='http://localhost:8081/#/Register2?shopId="+shopAuditLog.getShopId() +" '>修改链接</a>"
                        ,true);
        // 把对应审核的信息保存进对应的状态
        shopAuditLogMapper.add(shopAuditLog);
        shopMapper.update(shop);
    }

    /**
     * @Title: Repulse
     * @Description: 店铺拒绝邮件发送
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/12 11:18
     * @param:
     * @return:
     */
    @Override
    @Transactional
    public void Repulse(ShopAuditLog shopAuditLog) {
        // 查询出对应店铺数据
        Shop shop = shopMapper.getById(Long.valueOf(shopAuditLog.getShopId()));
        // 设置店铺状态为未审核
        shop.setState(0);
        // 设置审核人员id
        shopAuditLog.setAuditId(1); // 登录未实现先把操作人id写死
        // 调用mapper查询出对应的管理员
        Employee admin = employeeService.getById(shop.getAdminId());
        // 设置店铺拒绝状态
        shop.setState(-2);
        SendmailUtil.sendMail
                ("2353421050@qq.com"
                        ,admin.getEmail()
                        ,"店铺审核结果通知!"
                        ,"<h1>你好,你的店铺申请已被拒绝!</h1>" +
                                "拒绝原因:"+shopAuditLog.getNote()+"<br>"
                        ,true);
        // 把对应审核的信息保存进对应的状态
        shopAuditLogMapper.add(shopAuditLog);
        shopMapper.update(shop);
    }


    /**
     * @Title: activateShop
     * @Description: 店铺激活流程
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/12 11:18
     * @param:
     * @return:
     */
    @Override
    public void activateShop(Long id) {
        Shop shop = shopMapper.getById(id);
        // 判断店铺是否异常提交
        if (shop == null || shop.getId() != id){
            throw new BusinessException("店铺不存在!");
        }else if (shop.getState() == 2){
            throw new BusinessException("请忽重复激活!");
        }else if(shop.getState() == -1 || shop.getState() == -2){
            throw new BusinessException("店铺状态异常!");
        }
        // 激活店铺
        shop.setState(2);
        shopMapper.update(shop);
    }


    /**
     * @Title: amendShop
     * @Description: 店铺二次审核
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/12 11:18
     * @param:
     * @return:
     */
    @Override
    public void amendShop(Shop shop) {
        // 再次判断修改的信息是否合规
        String name = shop.getName();
        String tel = shop.getTel();
        String address = shop.getAddress();
        // 判断参数是否为空
        if (StringUtils.isEmpty(name.trim())
                || StringUtils.isEmpty(tel.trim())
                || StringUtils.isEmpty(address.trim()) )
        {
            throw new BusinessException("输入的参数不能为空,请重新输入");
        }
        // 调用百度AI审核判断参数是否合法
        if (!BaiDuAiCensorUtil.TextCensor(name)
                ||!BaiDuAiCensorUtil.TextCensor(tel)
                ||!BaiDuAiCensorUtil.TextCensor(address)) {
            throw new BusinessException("参数不合法,请重新输入!");
        }
        // 调用百度AI审核判断图片是否合法
        String imgPath = "http://123.207.27.208" + shop.getLogo(); // 拼接Fastdfs地址
        if (!BaiDuAiCensorUtil.ImgCensor(imgPath)) {
            throw new BusinessException("传入的图片不合法,请重新上传!");
        }
        // 判断是否重复提交
        if (shop.getState() == 2 || shop.getState() == 1){
            throw new BusinessException("请忽重复修改!");
        }
        // 设置状态为已审核
        shop.setState(1);
        // 最后在修改
        shopMapper.update(shop);
    }


    /**
     * @Title: batchAdd
     * @Description: 批量删除
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/8 20:28
     * @param:
     * @return:
     */
    @Override
    @Transactional
    public void batchAdd(List<Shop> shops) {
        shopMapper.batchAdd(shops);
    }


    /**
     * @Title: getShopEcharts
     * @Description: 展示报表数据
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/12 11:57
     * @param:
     * @return:
     */
    @Override
    public Map<String, Object> getShopEcharts() {
        // 查询所有状态与所有店铺状态的总数量
        List<ShopEchartsVo> shopEchartsVos =  shopMapper.getShopEcharts();
        // 店铺数量
        ArrayList<Integer> x = new ArrayList<>();
        // 所有状态
        ArrayList<String> y = new ArrayList<>();
        // 循环添加所有数据
        for (ShopEchartsVo vo : shopEchartsVos) {
            x.add(vo.getCount());
            Integer state = vo.getState();
            y.add( // 嵌套三目运算
                 state == 1 ? "审核通过":
                (state == 2 ? "已激活" :
                (state == 0 ? "未审核" :
                (state == -1 ? "驳回" : "拒绝" )))
            );
        }
        // 放入map中
        HashMap<String, Object> map = new HashMap<>();
        map.put("x",x);
        map.put("y",y);
        // 返回map
        return map;
    }
    // 继承基础服务层实现方法


}
